﻿using HPIT.RentHouse.Admin.Models;
using HPIT.RentHouse.Common;
using HPIT.RentHouse.DTO;
using HPIT.RentHouse.IService;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace HPIT.RentHouse.Admin.Controllers
{
    [Authorize]
    public class AdminUsersController : Controller
    {
        private IAdminUsersService _adminUsersService;
        private IRolesService _rolesService;
        private readonly ICitiesService _citiesService;
        public AdminUsersController(IAdminUsersService adminUsersService, IRolesService rolesService, ICitiesService citiesService)
        {
            _adminUsersService = adminUsersService;
            _rolesService = rolesService;
            _citiesService = citiesService;
        }

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult GetList(int start, int length, string name)
        {
            int rowCount = 0;
            var list = _adminUsersService.GetPageList(start, length, name, ref rowCount);
            PageModel pageModel = new PageModel();
            pageModel.data = list;
            pageModel.recordsTotal = rowCount;
            pageModel.recordsFiltered = rowCount;
            return Json(pageModel);
        }

        public ActionResult Create()
        {
            //1、获取城市
            List<CityDTO> cityList = _citiesService.GetList();
            //2、获取角色列表信息
            var roleList = _rolesService.GetList();
            ViewBag.cityList = cityList;
            ViewBag.roleList = roleList;
            return View();
        }
        [HttpPost]
        public ActionResult Create(AdminUserAddDTO dto)
        {
            if (!ModelState.IsValid)
            {
                return Json(new AjaxResult(ResultState.Error, MVCHelper.GetValidMsg(ModelState)));
            }
            else
            {
                var result = _adminUsersService.Add(dto);
                return Json(result);
            }
        }
    }
}